import axios from 'axios';
import qs from "qs"
import { config } from '@/config.js'
import { Message, Loading } from "element-ui"
const service = axios.create({
    // process.env.NODE_ENV === 'development' 来判断是否开发环境
    // easy-mock服务挂了，暂时不使用了
    // baseURL: 'https://www.easy-mock.com/mock/592501a391470c0ac1fab128',
    baseURL: config.BASE_URL,
    withCredentials: false,
    timeout: 5000,
});
let loading = null;
service.interceptors.request.use(
    config => {
        loading = Loading.service({
            text: "加载中",
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
        });
        config.headers["Content-Type"] =
            "application/x-www-form-urlencoded;charset=UTF-8"
        if (config.method === "post") {
            config.data = qs.stringify(config.data)
        }
        return config;
    },
    error => {
        console.log(error);
        return Promise.reject();
    }
);

service.interceptors.response.use(
    res => {
        loading.close()
        Message({
            message: '鸭儿哟',
            type: "success",
            duration: "1000"
        })
        return res.data;
    },
    err => {
        loading.close()
        console.log('err', err);
        return Promise.reject();
    }
);

export default service;
